{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 以下会使用到的API有：\n",
    "- 百度语音的语音识别api\n",
    "- 高德地图的地理编码api\n",
    "- 百度图像识别的人脸对比api"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、语音识别API的输入与输出示例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ### 解释：输入学号语音 → 通过api处理 → 输出学号的文字 → 调出对应用户的签到人照片（用于第三部的人脸对比）\n",
    " - 代码中用的为wav档的录音文件，在同目录下有放置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2222\n"
     ]
    }
   ],
   "source": [
    "from aip import AipSpeech\n",
    "\n",
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "APP_ID = '15001752'\n",
    "API_KEY = '5vE9IjkMMuiVxROd9UGe3EmW'\n",
    "SECRET_KEY = 'Fa2gxn1Ry15pS4HnGtaGqst3pNfchVxK'\n",
    "\n",
    "client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)\n",
    "\n",
    "# 读取文件\n",
    "def get_file_content(filePath):\n",
    "        with open(filePath, 'rb') as fp:\n",
    "            return fp.read()\n",
    "\n",
    "# 识别本地文件\n",
    "rtn = client.asr(get_file_content('test3.wav'), 'wav', 16000, {\n",
    "        'dev_pid': 1536,\n",
    "})\n",
    "\n",
    "print(rtn['result'][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、高德API的输出输出示例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 解释：输入上课地点 → 通过高德api处理 → 输出上课地点的经纬度 → 与签到人定位的地点进行对比，最终确定签到人是否在上课地点附近"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "113.392782,22.517645\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "#申请的key\n",
    "key='b69aaa6e1349970208a3cddc6f24d1b8'\n",
    "\n",
    "#传入地址，返回对应地址的经纬度信息\n",
    "def address(address):\n",
    "    url=\"http://restapi.amap.com/v3/geocode/geo?key=%s&address=%s\"%(key,address)\n",
    "    data=requests.get(url)\n",
    "    contest=data.json()\n",
    "    contest=contest['geocodes'][0]['location']\n",
    "    return contest\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    print(address(\"广东省中山大学南方学院教学楼4号\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、人脸对比API的输入与输出示例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 解释：输入签到人现场拍的照片和第一步中学号提取出来的照片 → 通过人脸对比api → 输出相似度的多少 → 最终通过相似度来判断是否为签到人。\n",
    "- 代码中用的为张一山的证件照和生活照作为对比，照片材料有放在pic的文件夹中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "检测结果：为签到者本人。 得分为: 89.79454041\n"
     ]
    }
   ],
   "source": [
    "from aip import AipFace\n",
    "import base64\n",
    "\n",
    "APP_ID = '14948345'\n",
    "API_KEY = '8YNSpNcSNGvn8G635UrFjKVl'\n",
    "SECRET_KEY = 'mnz07ulb5rkiKypgC2LsqEpFC5pWmObS'\n",
    "client = AipFace(APP_ID, API_KEY, SECRET_KEY)\n",
    "\n",
    "IMAGE_TYPE='BASE64'\n",
    "\n",
    "f1 = open('./pic/live.jpg','rb')\n",
    "f2 = open('./pic/IDcard.jpg','rb')\n",
    "#参数image：图像base64编码 分别base64编码后的2张图片数据\n",
    "img1 = base64.b64encode(f1.read())\n",
    "img2 = base64.b64encode(f2.read())\n",
    "image_1 = str(img1,'utf-8')\n",
    "image_2 = str(img2,'utf-8')\n",
    "\n",
    "ptr = client.match([\n",
    "       {\n",
    "                'image': image_1,\n",
    "                'image_type': 'BASE64',\n",
    "       },\n",
    "        {\n",
    "                'image': image_2,\n",
    "                'image_type': 'BASE64',\n",
    "        }\n",
    "])\n",
    "ptr = ptr['result']\n",
    "if ptr['score']<= 80:\n",
    "    print('检测结果：非签到者本人',\"得分为:\",ptr['score'])\n",
    "else:\n",
    "    print('检测结果：为签到者本人。',\"得分为:\",ptr['score'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
